#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>


typedef int BTDataType;

typedef struct TreeNode
{
	BTDataType val;
	struct TreeNode* left;
	struct TreeNode* right;
}BTNode;

BTNode* BuyNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}

	node->val = x;
	node->left = NULL;
	node->right = NULL;
	return node;
}

BTNode* CreatBinaryTree()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);

    node1->left = node2;

	return node1;
}

void reverseTree(struct TreeNode** left, struct TreeNode** right)
{
    if (*left == NULL && *right == NULL)
        return;

    if (*left == NULL && *right != NULL)
    {
        *left = *right;
        *right = NULL;
        reverseTree(&((*left)->left), &((*left)->right));
    }

    if (*left != NULL && *right == NULL)
    {
        *right = *left;
        *left = NULL;
        reverseTree(&((*right)->left), &((*right)->right));
    }

    int tmp = (*left)->val;
    (*left)->val = (*right)->val;
    (*right)->val = tmp;

    reverseTree(&((*left)->left), &((*right)->right));
    reverseTree(&((*left)->right), &((*right)->left));
}

struct TreeNode* invertTree(struct TreeNode* root)
{
    if (root == NULL)
        return NULL;

    reverseTree(&root->left, &root->right);
    return root;
}
int main()
{
    BTNode* root = CreatBinaryTree();
    invertTree(root);
    return 0;
}